home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / twodgrfx / stnclizr.sx < prev   
Encoding:
Text File  |  1996-05-21  |  5.3 KB  |  201 lines  |  [TEXT/ttxt]

  1. --<<<
  2.  
  3. -- Filename: stnclizr.sx
  4.  
  5. -- Other Files Required:  None
  6.  
  7. -- Purpose: 
  8. --         A ScriptX painting presenter, Stencilizer handles mouse events 
  9. --         to perform basic bit painting operations.  It caches results of 
  10. --         drawing in a bitmap, which could be saved in aStorageContainer.
  11. --         The Stencilizer class provides instance variables that could be 
  12. --         used with tool palettes to implement user selection of brushshape,  
  13. --         width, and color.   
  14.  
  15. -- Demonstrates classes from:
  16. --         2D Graphics: Stencil, Oval, Brush, Color, BitmapSurface
  17. --         Presenters: TwoDPresenter (draw method)
  18. --         Events: MouseDownEvent, MouseMoveEvent, MouseUpEvent,basic mouse 
  19. --        event handling
  20.  
  21. -- Specialized Classes: Stencilizer
  22.  
  23. -- Instructions to User:
  24. --         Run this script.  When the "ScriptX Stencilizer" window appears, 
  25. --        drag the mouse.  An array of ovals creates a red brush stroke along 
  26. --        the mouse path.  Change instance variables in the myStencilizer 
  27. --        object to set the brush, stencil, and width of the brush stroke. 
  28.  
  29. -- xx can delete the stencilizer from the window, add new stencilizers,
  30. -- put the stencilizer in a different window 
  31.  
  32. -- Authors:  Jim Inscore and Jocelyn Becker, Kaleida Technical Publications
  33.  
  34.  
  35. -- <<<
  36.  
  37. class Stencilizer (TwoDPresenter)    
  38.     instance variables   
  39.         drawingCache     -- bitmap for caching previous strokes
  40.         strokeClass     -- class of Stencil used for the current brush
  41.         currentBrush     -- Brush instance now used to paint with
  42.         
  43.         strokeWidth        -- Half the width of the brush stroke
  44.         mouseDown         -- event interests managed by theStencilizer
  45.         mouseMoved 
  46.         mouseUp
  47. end
  48.  
  49.  
  50.  
  51. method renderStroke self {class Stencilizer} theInterest theEvent ->
  52. (
  53.     -- get the current mouse point
  54.     local thisPoint := theEvent.localCoords
  55.     local pointx := thisPoint.x
  56.     local pointy := thisPoint.y
  57.     local strokeWidth := self.strokeWidth
  58.     
  59.     -- create a stencil 
  60.     local thisStencil := (new self.strokeClass \
  61.             x1:(pointx  - strokeWidth)\
  62.             y1:(pointy  - strokeWidth) \
  63.             x2:(pointx + strokeWidth) \
  64.             y2:(pointy + strokeWidth))
  65.             
  66.             -- render the stencil to the bitmap surface
  67.     fill self.drawingcache \
  68.         thisStencil \
  69.         self.boundary \
  70.         identityMatrix \
  71.         self.currentBrush
  72.  
  73.     -- call notifyChanged to tell the compositor
  74.     -- that the stencilizer has changed
  75.     notifyChanged self true
  76.     
  77.     -- accept the event
  78.     true
  79. )
  80.  
  81.  
  82. -- this method handles mouse down events
  83. method beginStroke self {class Stencilizer} theInterest theEvent ->
  84. (
  85.     -- put the mouseMoved and mouseUp events on duty
  86.     addEventInterest self.mouseMoved
  87.     addEventInterest self.mouseUp
  88.     
  89.     -- render the stroke
  90.     renderStroke self theInterest theEvent
  91. )
  92.  
  93.  
  94. -- this method handles mouse up events
  95. method endStroke self {class Stencilizer} theInterest theEvent ->
  96. (
  97.     -- remove the mouseMoved and mouseUp events from duty
  98.     removeEventInterest self.mouseMoved
  99.     removeEventInterest self.mouseUp
  100.     
  101.     -- render the stroke
  102.     renderStroke self theInterest theEvent
  103. )
  104.  
  105. -- this method initializes the Stencilizer
  106. method init self {class Stencilizer} #rest args ->
  107. (
  108.     apply nextMethod self args
  109.     
  110.     -- define the brush characteristics
  111.     self.currentBrush :=  new Brush color:redColor
  112.     self.strokeWidth := 4
  113.     self.strokeClass := Oval
  114.     -- make the drawing cache be fairly big
  115.     self.drawingCache := new BitmapSurface bbox:(new rect x2:600 y2:400)
  116.  
  117.     local theMouseDevice := new MouseDevice
  118.     
  119.     -- set up the mouse event interests
  120.     self.mouseDown := new MouseDownEvent
  121.     self.mouseDown.eventReceiver := beginStroke
  122.     self.mouseDown.authorData := self
  123.     self.mouseDown.device := theMouseDevice
  124.     self.mouseDown.presenter := self
  125.         
  126.     self.mouseMoved := new MouseMoveEvent
  127.     self.mouseMoved.eventReceiver := renderStroke
  128.     self.mouseMoved.authorData := self
  129.     self.mouseMoved.device := theMouseDevice
  130.     self.mouseMoved.presenter := self
  131.         
  132.     self.mouseUp := new MouseUpEvent
  133.     self.mouseUp.eventReceiver := endStroke
  134.     self.mouseUp.authorData := self
  135.     self.mouseUp.device := theMouseDevice
  136.     self.mouseUp.presenter := self
  137.     
  138.     -- make sure stencilizer gets mouse up, even outside its boundary
  139.     self.mouseUp.matchedInterest := self.mouseDown
  140.     
  141.     -- at the beginning, put the mouseDown event on duty
  142.     addEventInterest self.mouseDown
  143. )
  144.  
  145.  
  146.  
  147. -- draws the current contents of the Stencilizer to a surface
  148. -- this method is called automatically by the compositor 
  149. method draw self {class Stencilizer} surface clip -> 
  150. (
  151.     -- transfers the drawing cache to the surface
  152.     transfer surface \
  153.         self.drawingCache \
  154.         self.globalBoundary \
  155.         self.globalTransform
  156.     
  157.     -- draw the outline of the stencilizer's area
  158.     stroke surface \
  159.         self.boundary \
  160.         self.globalBoundary \
  161.         self.globalTransform \
  162.         blackbrush
  163. )
  164.  
  165.  
  166.  
  167. -- to save the image created, save the stencilizer
  168. -- to create a new image, create a new stencilizer
  169.  
  170. -- sets up a simple example of the stencilizer class
  171. global myWindow := new Window \
  172.     boundary:(new Rect x2:400 y2:300) \
  173.     name:"ScriptXStencilizer"
  174.  
  175. global myStencilizer := new Stencilizer \
  176.     boundary:(new Rect x2:400 y2:300) 
  177. append myWindow myStencilizer
  178.  
  179. myWindow.x := 50
  180. myWindow.y := 50
  181. show myWindow
  182.  
  183.  
  184.  
  185. global redBrush := myStencilizer.currentBrush
  186. global yellowBrush := new Brush color:yellowColor
  187. global greenBrush := new Brush color:greenColor
  188. global blueBrush := new Brush color:blueColor
  189.  
  190.  
  191. -- try the following changes
  192.  
  193. -->>>
  194.  
  195. myStencilizer.strokeWidth := 10
  196. myStencilizer.strokeClass := Rect
  197. myStencilizer.currentBrush := yellowBrush
  198.  
  199.  
  200.  
  201.